package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JFileChooser;

import command.Invoker;

import model.Image;

import view.MenuFrame;

public class MenuController {
	
	private Image image;
	private MenuFrame menu;
	private Invoker invoker;
	
	public MenuController(MenuFrame menu, Image image) {
		
		this.image = image;
		this.menu = menu;
		
		
		menu.addOuvrirMenuListener(new OuvrirMenuListener());
		menu.addFermerMenuListener(new FermerMenuListener());
		menu.addQuitterMenuListener(new QuitterMenuListener());
		menu.addAnnulerMenuListener(new AnnulerMenuListener());
		menu.addRefaireMenuListener(new RefaireMenuListener());
		menu.addCopierMenuListener(new CopierMenuListener());
		menu.addCollerMenuListener(new CollerMenuListener());
		menu.addCouperMenuListener(new CouperMenuListener());
	}
	
	class OuvrirMenuListener implements ActionListener {
		 
		@Override
		public void actionPerformed(ActionEvent arg0) {
		
			JFileChooser chooser = new JFileChooser();
			int validValue = chooser.showOpenDialog(menu);
			if(validValue == JFileChooser.APPROVE_OPTION)
				try {
					image.setImagePath(chooser.getSelectedFile().getPath());
				} catch (IOException e) {
					e.printStackTrace();
				}
			invoker = Invoker.getInstance();
			invoker.reset();
		}
	}
	
	class FermerMenuListener implements ActionListener {
		 
		@Override
		public void actionPerformed(ActionEvent arg0) {
			image.disable();
			invoker = Invoker.getInstance();
			invoker.reset();
		}
	}
	
	class QuitterMenuListener implements ActionListener {
		 
		@Override
		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);
		}
	}
	
	class AnnulerMenuListener implements ActionListener {
		 
		@Override
		public void actionPerformed(ActionEvent arg0) {
			invoker = Invoker.getInstance();
			invoker.undo();
		}
	}
	
	class RefaireMenuListener implements ActionListener {
		 
		@Override
		public void actionPerformed(ActionEvent arg0) {
			invoker = Invoker.getInstance();
			invoker.redo();
		}
	}
	
	class CopierMenuListener implements ActionListener {
		 
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO
		}
	}
	
	class CollerMenuListener implements ActionListener {
		 
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO
		}
	}
	
	class CouperMenuListener implements ActionListener {
		 
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO
		}
	}

}